package com.coolwen.controller;

import com.alibaba.fastjson.JSON;
import com.coolwen.entity.Address;
import com.coolwen.entity.Resource;
import com.coolwen.entity.Role;
import com.coolwen.entity.User;
import com.coolwen.service.AddressService;
import com.coolwen.service.RoleService;
import com.coolwen.service.UserService;
import com.sun.jndi.cosnaming.IiopUrl;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 2016/6/29.
 */
@Controller
@RequestMapping("/user/address/")
public class AddressController {

    private Logger logger = Logger.getLogger(getClass());

    @Autowired
    private AddressService addService;
    @Autowired
    private UserService userService;


    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add(Model model) {
        Subject currentUser = SecurityUtils.getSubject();
        User loginUser = (User) currentUser.getPrincipals().getPrimaryPrincipal();
        User u = userService.load(loginUser.getId());
        u.setAddresses(addService.listByUid(u.getId()));
        model.addAttribute("address", new Address());
        model.addAttribute("user", u);
        return "address/add";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(Address address, BindingResult br, String userId) {
        logger.info("address添加:" + JSON.toJSON(address));
        if (br.hasErrors()) {
            //如果有错误直接跳转到add视图
            return "address/add";
        }
        address.setUserId(Integer.parseInt(userId));
        //       logger.info("address添加:" + JSON.toJSON(address));
        addService.add(address);
        return "redirect:/admin/user/show";
    }


    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable int id, Model model) {
        Address address = addService.load(id);
        logger.info("address添加:" + JSON.toJSON(address));
        model.addAttribute("address", address);
        return "address/update";
    }

    @RequestMapping(value = "update/{id}", method = RequestMethod.POST)
    public String update(@PathVariable int id, Address address, BindingResult br, Model model) {
        if (br.hasErrors()) {
            return "user/update";
        }
//        logger.info("address更新:" + JSON.toJSON(address));
        addService.update(address);
        return "redirect:/admin/user/show";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable int id) {
        addService.delete(id);
        return "redirect:/admin/user/show";
    }


}
